package com.terminal;




import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


public class Main extends Activity {
	/** Called when the activity is first created. */
	Button btn = null;
	Button btn1 = null;
	public String Server = "192.168.1.200"; 
	EditText txtPort;
	EditText txtServer;
	Spinner s1;
	


	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		this.setTitle(R.string.app_name);
		setContentView(R.layout.main);
		
		
		txtServer = (EditText) findViewById(R.id.server);
		txtPort = (EditText) findViewById(R.id.port);
		btn = (Button) findViewById(R.id.btn);
		btn1 = (Button) findViewById(R.id.btn1);
		
		s1 = (Spinner) findViewById(R.id.fontsize);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.sizes, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapter);
		

		
		txtPort.setText("23");
		txtServer.setText("192.168.1.200");
		

		
		btn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				ExitApplication();
			}
		});

		btn1.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				OpenTerminal();
			}
		});

	}

	private void OpenTerminal() {
		if(txtServer.getText().toString().trim().equals(""))
		{
			//AlertDialog.show(this, "Error", "Server name is empty!", "Cancel", true);
			Toast.makeText(Main.this,"Server name is empty!", Toast.LENGTH_LONG).show();
			return;
		}
		String sPort = txtPort.getText().toString().trim();
		boolean isBadPort = false;
		int iPort = 0;
		try
		{
			iPort = Integer.parseInt(sPort);
		}
		catch(Exception ex)
		{
			isBadPort = true;
		}
		if(iPort <=0 || iPort >= 65535)
		{
			isBadPort = true;
		}
		if(isBadPort)
		{
			//AlertDialog.show(this, "Error", "Bad port number!", "Cancel", true);
			Toast.makeText(Main.this,"Bad port number!", Toast.LENGTH_LONG).show();
			return;
		}
		
		Editor ed = this.getPreferences(0).edit();
		ed.putString("server", txtServer.getText().toString());
		ed.putString("port", txtPort.getText().toString());
		int sel = s1.getSelectedItemPosition();
		ed.putInt("fontsize", sel);
		
		ed.commit();		
		
		Intent i = new Intent(this, TerminalActivity.class);
		i.putExtra("Server", txtServer.getText().toString());
		i.putExtra("Port", iPort);
		i.putExtra("FontSize", getFontSizeFromItem(sel));
		startActivity(i);
	}
	
	private int getFontSizeFromItem(int si)
	{
		switch(si)
		{
		case 0:
			return 6;
		case 1:
			return 7;
		case 2:
			return 8;
		case 3:
			return 10;
		case 4:
			return 12;
		case 5:
			return 14;
	
		}
		return 10;
	}
	
	private void ExitApplication()
	{
		this.finish();
	}

}




